// pages/user/index.js
const cloudHelper = require('../../helper/cloud_helper.js');
const util = require('../../utils/util.js');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        openId: '',
        user_info: {
            open_id: '',
            avatar: '',
            nickname: '',
            phone: '',
            sex: '',
            is_admin: '',
        },
        order_status_1_cnt: 0, // 待支付订单数
        order_status_2_cnt: 0, // 待出行订单数
        customer_service_weixin: '', // 客服微信
        developer: '', // 开发者
    },

    onLoad(option) {
        this.getConfig();
        this.getUserInfo();
        this.getOrderCnt();
        if (option && option['spread_openid']) {
            wx.setStorageSync("spread_openid", option['spread_openid']);
        }
    },

    onPullDownRefresh() {
        this.getUserInfo();
        this.getOrderCnt();
        wx.stopPullDownRefresh();
    },

    // 分享
    onShareAppMessage: function () {
        return {
            title: '',
            path: 'pages/user/index?spread_openid=' + this.data.user_info.open_id
        }
    },

    // 显示分享
    onShowWeixinCustomer() {
        let content = this.data.customer_service_weixin;
        wx.showModal({
            title: '客服微信',
            content: content,
            cancelText: '取消',
		    confirmText: '复制号码',
            success: (res) => {
                if (res.confirm) {
                    wx.setClipboardData({ //设置系统剪贴板的内容
                        data: content,
                        success(res) {
                            wx.showToast({
                                title: '复制成功',
                            })
                        }
                    })
                }
            }
        })
    },

    // 获取配置
    async getConfig() {
        const config = await cloudHelper.callCloudSumbitAsync('api/home/get_config', {
            keys: ['customer_service_weixin', 'developer']
        });
        if (config && config.data && config.data.list) {
            if (config.data.list.customer_service_weixin) {
                this.setData({
                    customer_service_weixin: config.data.list.customer_service_weixin.value
                })
            }
            if (config.data.list.developer) {
                this.setData({
                    developer: config.data.list.developer.value
                })
            }
        }
    },

    // 获取用户信息
    async getUserInfo() {
        let userInfo = await cloudHelper.callCloudData('api/user/get_info');
        if (userInfo.open_id) {
            wx.setStorageSync("user_info", userInfo);
            this.setData({
                user_info: {
                    ...this.data.user_info,
                    open_id: userInfo.open_id,
                    nickname: userInfo.nickname,
                    avatar: userInfo.avatar,
                    phone: util.encryptPhone(userInfo.phone),
                    sex: userInfo.sex,
                    is_admin: userInfo.is_admin
                }
            });
        }
    },

    // 获取订单数
    async getOrderCnt() {
        let order_status_1_cnt = 0;
        let order_status_2_cnt = 0;
        const res_1 = await cloudHelper.callCloudData('api/order/list', {
            status: 2,
            page: 1,
            size: 1
        });
        if (res_1) {
            order_status_1_cnt = res_1.total;
        }
        const res_2 = await cloudHelper.callCloudData('api/order/list', {
            status: 1,
            page: 1,
            size: 1
        });
        if (res_2) {
            order_status_2_cnt = res_2.total;
        }
        this.setData({
            order_status_1_cnt,
            order_status_2_cnt
        });
    },

    // 检测登陆
    checkLogin() {
        if (this.data.user_info.open_id) {
            // 跳转个人资料
            this.goUserInfo();
        } else {
            // 跳转登陆页
            wx.navigateTo({
                url: '/pages/login/index',
            });
        }
    },

    // 更换头像
    async bindAvatar(e) {
        let avatarUrl = e.detail.avatarUrl;
        wx.showLoading({
            title: '头像上传中'
        });
        let pic = await cloudHelper.transTempPicOne(avatarUrl, 'user/', '', false);
        let user_info = this.data.user_info;
        user_info.user_pic = pic;
        // 修改数据库头像信息
        // todo ...
        this.setData({
            user_info
        });
        wx.hideLoading();
    },

    // 跳转分享小程序页面
    gotoWxCodePage() {
        wx.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        })
    },

    // 跳转个人资料
    goUserInfo() {
        wx.navigateTo({
            url: `/pages/other/user-info/index`,
        });
    },

    // 复制
    onCopy(e) {
        let content = e.currentTarget.dataset.content;
        wx.setClipboardData({
            data: content,
            success(res) {
                wx.showToast({
                    title: '复制成功',
                })
            }
        })
    }
});